package com.sy.module_clockin.recycler

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.provider.Settings
import android.util.TypedValue
import androidx.fragment.app.Fragment
import androidx.fragment.app.FragmentActivity
import com.blankj.utilcode.util.ToastUtils
import com.fenghuajueli.libbasecoreui.dialog.CommonTipsDialog
import com.permissionx.guolindev.PermissionX
import java.io.File


fun Context.dp2px(value: Int): Int = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value.toFloat(), resources.displayMetrics).toInt()

fun Context.dp2px(value: Float) = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, value, resources.displayMetrics)


/**
 * 权限申请
 * api 'com.guolindev.permissionx:permissionx:1.6.0'
 */
fun Fragment.requestPermission(
        permissions: List<String>,
        explainMessege: String = "需要您同意以下权限才能正常使用",
        requestReasonDiaLeftButtonStr: String? = "取消",
        requestReasonDiaRightButtonStr: String = "确定",
        forwardToSettingMessege: String = "去设置",
        forwardToSettingLeftButtonStr: String? = "取消",
        forwardToSettingRightButtonStr: String = "去设置开启权限",
        deniedBlock: (() -> Unit)? = null,
        grantedBlock: (() -> Unit)? = null
) {
    PermissionX.init(this)
            .permissions(permissions)
            .explainReasonBeforeRequest()
            .onExplainRequestReason { scope, deniedList ->
                scope.showRequestReasonDialog(
                        deniedList,
                        explainMessege,
                        requestReasonDiaRightButtonStr,
                        requestReasonDiaLeftButtonStr
                )
            }
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(
                        deniedList,
                        forwardToSettingMessege,
                        forwardToSettingRightButtonStr,
                        forwardToSettingLeftButtonStr
                )
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    grantedBlock?.invoke()
                } else {
                    deniedBlock?.invoke()
                }
            }
}

/**
 * 权限申请
 * api 'com.guolindev.permissionx:permissionx:1.6.0'
 */
fun FragmentActivity.requestPermission(
        permissions: List<String>,
        explainMessege: String = "需要您同意以下权限才能正常使用",
        requestReasonDiaLeftButtonStr: String? = "取消",
        requestReasonDiaRightButtonStr: String = "确定",
        forwardToSettingMessege: String = "去设置",
        forwardToSettingLeftButtonStr: String? = "取消",
        forwardToSettingRightButtonStr: String = "去设置开启权限",
        deniedBlock: (() -> Unit)? = null,
        grantedBlock: (() -> Unit)? = null
) {
    PermissionX.init(this)
            .permissions(permissions)
            .explainReasonBeforeRequest()
            .onExplainRequestReason { scope, deniedList ->
                scope.showRequestReasonDialog(
                        deniedList,
                        explainMessege,
                        requestReasonDiaRightButtonStr,
                        requestReasonDiaLeftButtonStr
                )
            }
            .onForwardToSettings { scope, deniedList ->
                scope.showForwardToSettingsDialog(
                        deniedList,
                        forwardToSettingMessege,
                        forwardToSettingRightButtonStr,
                        forwardToSettingLeftButtonStr
                )
            }
            .request { allGranted, grantedList, deniedList ->
                if (allGranted) {
                    grantedBlock?.invoke()
                } else {
                    deniedBlock?.invoke()
                }
            }
}


/**
 * 跳转到权限设置界面
 */
fun Context.actionToApplicationDetailSetting() {
    runCatching {
        startActivity(Intent().apply {
            flags = Intent.FLAG_ACTIVITY_NEW_TASK
            action = Settings.ACTION_APPLICATION_DETAILS_SETTINGS
            data = Uri.fromParts("package", packageName, null)
        })
    }
}

fun Context.showPermissionRefuseDialog(){
    CommonTipsDialog(this,"是否跳转到设置开启权限","取消","去开启").apply {
        setConfirmListener { actionToApplicationDetailSetting() }
        show()
    }
}

/**
 * 拷贝文件到缓存目录
 */
fun Context.copyFileToCacheDir(cacheFile: File, sourceUri: Uri) {
    runCatching {
        cacheFile.createNewFile()
        cacheFile.outputStream().use {
            it.write(contentResolver.openInputStream(sourceUri)?.readBytes())
            it.flush()
        }
    }.onFailure {
        ToastUtils.showShort("获取图片失败")
    }

}

/**
 * 跳转应用市场
 */
fun Context.actionToAppMarket() {
    runCatching {
        val uri = Uri.parse("market://details?id=${packageName}")
        val intent = Intent(Intent.ACTION_VIEW, uri)
        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
        startActivity(intent)
    }.onFailure {
        ToastUtils.showShort("您的手机未安装应用市场！")
    }
}



